home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / Sources / SLResour.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  8.9 KB  |  316 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLResour.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWRESFI_K
  13. #include "FWResFil.k"
  14. #endif
  15.  
  16. #ifndef FWRESFI_H
  17. #include "FWResFil.h"
  18. #endif
  19.  
  20. #ifndef   FWSTRS_H
  21. #include "FWStrs.h"
  22. #endif
  23.  
  24. #ifndef   FWFILESP_H
  25. #include "FWFileSp.h"
  26. #endif
  27.  
  28. #ifndef FWEXCDEF_H
  29. #include "FWExcDef.h"
  30. #endif
  31.  
  32. #ifndef   FWPRIDEB_H
  33. #include "FWPriDeb.h"
  34. #endif
  35.  
  36. #ifndef   FWMEMMGR_H
  37. #include "FWMemMgr.h"
  38. #endif
  39.  
  40. #ifndef FWEXCEPT_H
  41. #include "FWExcept.h"
  42. #endif
  43.  
  44.  
  45. #ifdef FW_BUILD_MAC
  46. #pragma segment fwresour
  47. #endif
  48.  
  49. /*
  50.  *  This file was generated by the SOM Compiler.
  51.  *  Generated using: 
  52.  *      SOM Emitter emitxtm.dll: 2.33
  53.  */
  54.  
  55. #define FW_OResource_Class_Source
  56. class FW_CPrivResourceRep;
  57. #include "SLResour.xih"
  58.  
  59. //========================================================================================
  60. // struct FW_CPrivResourceRep
  61. //    This struct contains the body of the 
  62. //========================================================================================
  63.  
  64. class FW_CPrivResourceRep
  65. {
  66. public:
  67.     FW_DECLARE_AUTO(FW_CPrivResourceRep)
  68.  
  69.     FW_CPrivResourceRep(Environment* ev,
  70.                         FW_OResourceFile* file,
  71.                         FW_ResourceId resourceId,
  72.                         FW_ResourceType resourceType);
  73.  
  74.     ~FW_CPrivResourceRep();
  75.  
  76.     FW_PResourceFile    fResourceFile;        // The resources file this resource originated from.
  77.     FW_ResourceId        fResourceID;        // The resource ID of the resource.
  78.     FW_ResourceType        fResourceType;        // The type of the resource.
  79.     FW_ResourceHandle    fResourceHandle;    // The native resource handle.
  80.     unsigned long        fResourceSize;        // The size of the resource, in bytes.
  81.  
  82. #ifdef FW_BUILD_MAC
  83.     unsigned short        fLockCount;            // Incremented by GetData, decremented by ReleaseData
  84.     FW_PlatformHandle    fResourceData;        // Resource data is stored here.
  85. #endif
  86.  
  87. #ifdef FW_BUILD_WIN
  88.     HGLOBAL                fLoadedHandle;
  89. #endif
  90. };
  91.  
  92.  
  93. FW_DEFINE_AUTO(FW_CPrivResourceRep)
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // FW_CPrivResourceRep::FW_CPrivResourceRep
  97. //----------------------------------------------------------------------------------------
  98.  
  99. inline FW_CPrivResourceRep::FW_CPrivResourceRep(Environment* ev,
  100.                                                 FW_OResourceFile* file,
  101.                                                 FW_ResourceId resourceId,
  102.                                                 FW_ResourceType resourceType) :
  103.     fResourceFile(ev, file),
  104.     fResourceID(resourceId),
  105.     fResourceType(resourceType),
  106.     fResourceHandle(0)
  107. {
  108. #ifdef FW_BUILD_MAC
  109.     fLockCount = 0;
  110.     fResourceData = 0;
  111.     {
  112.         FW_CMacResLoadFalse dontLoadResource;
  113.         fResourceHandle = fResourceFile->GetResourceHandle(ev, fResourceID, fResourceType);
  114.     }
  115. #elif defined FW_BUILD_WIN
  116.     fResourceHandle = fResourceFile->GetResourceHandle(ev, fResourceID, fResourceType);
  117. #endif
  118.  
  119. #if defined FW_BUILD_MAC
  120.     fResourceSize = ::GetResourceSizeOnDisk(fResourceHandle);
  121. #elif defined FW_BUILD_WIN
  122.     fResourceSize = ::SizeofResource(fResourceFile->PrivGetResourceFileID(ev), fResourceHandle);
  123. #endif
  124.  
  125.     FW_END_CONSTRUCTOR
  126. }
  127.  
  128.  
  129. //----------------------------------------------------------------------------------------
  130. // FW_CPrivResourceRep::~FW_CPrivResourceRep
  131. //----------------------------------------------------------------------------------------
  132.  
  133. inline FW_CPrivResourceRep::~FW_CPrivResourceRep()
  134. {
  135.     FW_START_DESTRUCTOR
  136.  
  137. #ifdef FW_BUILD_MAC
  138.     FW_ASSERT(fLockCount == 0);
  139.     if (fResourceData != 0)
  140.         FW_CMemoryManager::FreeSystemHandle(fResourceData);
  141. #endif
  142.     FW_SOMEnvironment ev;
  143.     fResourceFile->ReleaseResourceHandle(ev, fResourceHandle);
  144. }
  145.  
  146.  
  147. //========================================================================================
  148. // CLASS FW_OResource
  149. //========================================================================================
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // FW_OResource__InitFromFile
  153. //  Acquires the resource handle
  154. //----------------------------------------------------------------------------------------
  155.  
  156. SOM_Scope void  SOMLINK FW_OResource__InitFromFile(FW_OResource *somSelf, Environment *ev,
  157.         FW_OResourceFile* file,
  158.         FW_ResourceId resourceId,
  159.         FW_ResourceType resourceType)
  160. {
  161.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  162.  
  163.     FW_SOM_TRY
  164.     {
  165.         somThis->fRep = FW_NEW(FW_CPrivResourceRep, (ev, file, resourceId, resourceType));
  166.     }
  167.     FW_SOM_CATCH
  168. }
  169.  
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // FW_OResource__AcquireData
  173. //  Lock the resource and return a pointer to the data.
  174. //  Client assumes responsiblity to call ReleaseData.
  175. //----------------------------------------------------------------------------------------
  176.  
  177. SOM_Scope void*  SOMLINK FW_OResource__AcquireData(FW_OResource *somSelf, Environment *ev)
  178. {
  179.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  180.  
  181.     FW_SOM_TRY
  182.     {
  183.         FW_CPrivResourceRep& rep = *somThis->fRep;
  184.         void *result;
  185.  
  186.         FW_ASSERT(rep.fResourceHandle != 0);
  187. #if defined FW_BUILD_MAC
  188.         FW_ASSERT(rep.fLockCount >= 0);
  189.         if (rep.fLockCount == 0)
  190.         {
  191.             if (rep.fResourceData == 0)
  192.             {
  193.                 // ODNewHandle throws kODErrOutOfMemory if it fails
  194.                 rep.fResourceData = FW_CMemoryManager::AllocateSystemHandle(rep.fResourceSize);
  195.     
  196.                 // Lock the handle, read the resource data
  197.                 FW_CMemoryManager::LockSystemHandle(rep.fResourceData);
  198.                 ::ReadPartialResource(rep.fResourceHandle, 0, *rep.fResourceData, rep.fResourceSize);
  199.     
  200.                 // Throw an exception if the read failed.
  201.                 FW_FailOnError(::ResError());
  202.             } 
  203.             else
  204.             {
  205.                 FW_CMemoryManager::LockSystemHandle(rep.fResourceData);
  206.             }
  207.         }
  208.         ++rep.fLockCount;
  209.         FW_ASSERT(*rep.fResourceData != 0);
  210.         result = *rep.fResourceData;
  211. #elif defined FW_BUILD_WIN
  212.         rep.fLoadedHandle = ::LoadResource(rep.fResourceFile->PrivGetResourceFileID(ev), rep.fResourceHandle);
  213.         result = ::LockResource(rep.fLoadedHandle);
  214. #endif
  215.  
  216.         return result;
  217.     }
  218.     FW_SOM_CATCH
  219.  
  220.     return 0;
  221. }
  222.  
  223.  
  224. //----------------------------------------------------------------------------------------
  225. // FW_OResource__ReleaseData
  226. //  Unlock the resource.
  227. //----------------------------------------------------------------------------------------
  228.  
  229. SOM_Scope void  SOMLINK FW_OResource__ReleaseData(FW_OResource *somSelf, Environment *ev)
  230. {
  231.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  232.  
  233.     FW_SOM_TRY
  234.     {
  235.         FW_CPrivResourceRep& rep = *somThis->fRep;
  236.  
  237. #if defined FW_BUILD_MAC
  238.         FW_ASSERT(rep.fLockCount > 0);
  239.         if (--rep.fLockCount == 0)
  240.         {
  241.             FW_CMemoryManager::UnlockSystemHandle(rep.fResourceData);
  242.             // (JEL) ??? Should we HPurge too?  My feeling is, tell developers they should
  243.             // generally set all resources to purgeable, but allow them to set a resource
  244.             // nonpurgeable for performance reasons.  If we always did an HPurge, we take
  245.             // control from the developer, for no good reason.
  246.         }
  247. #elif defined FW_BUILD_WIN16
  248.         ::UnlockResource(rep.fLoadedHandle);
  249. #endif
  250.     }
  251.     FW_SOM_CATCH
  252. }
  253.  
  254.  
  255. //----------------------------------------------------------------------------------------
  256. // FW_OResource__GetSize
  257. //  Return the size of this resource in bytes.
  258. //----------------------------------------------------------------------------------------
  259.  
  260. SOM_Scope unsigned long  SOMLINK FW_OResource__GetSize(FW_OResource *somSelf, Environment *ev)
  261. {
  262. FW_UNUSED(ev);
  263.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  264.  
  265.     return somThis->fRep->fResourceSize;
  266. }
  267.  
  268.  
  269. //----------------------------------------------------------------------------------------
  270. // FW_OResource__GetResourceHandle
  271. //  Return the platform (native) handle.
  272. //  This method should be used with caution, since it reveals platform specifics,
  273. //  and allows violation of internal lock counts, etc.
  274. //----------------------------------------------------------------------------------------
  275.  
  276. SOM_Scope FW_ResourceHandle  SOMLINK FW_OResource__GetResourceHandle(FW_OResource *somSelf, Environment *ev)
  277. {
  278. FW_UNUSED(ev);
  279.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  280.  
  281.     return somThis->fRep->fResourceHandle;
  282. }
  283.  
  284.  
  285. //----------------------------------------------------------------------------------------
  286. // FW_OResource__somInit
  287. //----------------------------------------------------------------------------------------
  288.  
  289. SOM_Scope void  SOMLINK FW_OResource__somInit(FW_OResource *somSelf)
  290. {
  291.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  292.  
  293.     FW_OResource_parent_FW_ORefCount_somInit(somSelf);
  294.  
  295.     somThis->fRep = 0;
  296. }
  297.  
  298.  
  299. //----------------------------------------------------------------------------------------
  300. // FW_OResource__somUninit
  301. //----------------------------------------------------------------------------------------
  302.  
  303. SOM_Scope void  SOMLINK FW_OResource__somUninit(FW_OResource *somSelf)
  304. {
  305.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  306.  
  307.     FW_SOM_UNINIT_TRY
  308.     {
  309.         delete somThis->fRep;
  310.         FW_OResource_parent_FW_ORefCount_somUninit(somSelf);
  311.     }
  312.     FW_SOM_UNINIT_CATCH
  313. }
  314.  
  315.  
  316.